<?php
class Data extends CI_Model {
    public function __construct(){
        parent::__construct();
    }
    public function get_one($table, $where=1, $field='*', $order=1,$show_sql=0){
        $this->db->select($field);
        $this->db->order_by($order);
        $query=$this->db->get_where($table,$where);
        if($show_sql==1){
            echo $this->db->last_query();
        }
        return $query->row_array();
    }

    public function get_all($table, $where=1, $field='*', $order=1,$show_sql=0){
        $this->db->select($field);
        $this->db->order_by($order);
        $query=$this->db->get_where($table,$where);
        if($show_sql==1){
            echo $this->db->last_query();
        }
        return $query->result_array();
    }
	
	public function get_limit($table, $where=1, $field='*', $order=1,$start,$limit,$show_sql=0){
        $query=$this->db->query('select '.$field.' from '.$table.' where '.$where.' order by '.$order.' limit '.$start.','.$limit);
        if($show_sql==1){
            echo $this->db->last_query();
        }
        return $query->result_array();
    }

    public function get_row_count($table, $where=1,$show_sql=0){	//返回总记录数
        $query = $this->db->query("select count(*) as row_count from $table where $where");
        $info  = $query->row_array();
        if($show_sql==1){
            echo $this->db->last_query();
        }
        return $info['row_count'];
    }
	
    public function get_value($table, $where=1, $field, $order=1,$show_sql=0){
        $this->db->select($field);
        $this->db->order_by($order);
        $query=$this->db->get_where($table,$where);
        if($show_sql==1){
            echo $this->db->last_query();
        }
        $info=$query->row_array();
        return $info[$field];
    }
    public function get_max($table, $where=1, $field){
        $query = $this->db->query("select max($field) as max_value from $table where $where");
        $info  = $query->row_array();
        return $result['max_value'];
    }

    public function insert($table,$array){
        $this->db->insert($table,$array);
    }

    public function update($table,$where,$array,$show_sql=0){
        $this->db->where($where);
        $this->db->update($table,$array);
        if($show_sql==1){
            echo $this->db->last_query();
        }
    }

    public function delete($table,$where){
        $this->db->where($where);
        $this->db->delete($table);
    }
}